package com.yesorno.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yesorno.constants.Constants;
import com.yesorno.model.HttpResponseObject;
import com.yesorno.ui.CustomTitleActivity;
import com.yesorno.utils.HttpUtils;

public class PersonalMessageActivity extends CustomTitleActivity {

	private Button goBack;// 返回
	private Button reCall;// 回复

	private ListView user_message_listview;

	HallBtnsClickListeners rbc = new HallBtnsClickListeners();

	List<Map<String, Object>> list;// listview对应的结果集
	ImgSimpleAdapter adapter;

	private long userId;

	/**
	 * 大厅页面按钮的监听类
	 * 
	 * @author samoin
	 * @date 2011-07-06
	 * @version 0.1
	 * 
	 */
	class HallBtnsClickListeners implements OnClickListener {
		@Override
		public void onClick(View v) {

			if (v == goBack) {
				Intent intent = new Intent();
				PersonalMessageActivity.this.setResult(RESULT_OK, intent);
				PersonalMessageActivity.this.finish();
			}
			if (v == reCall) {

			}
		}

	}

	/**
	 * 菜单回传的处理
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) {
		case RESULT_OK:
			String type = data.getStringExtra("type");
			String selectIndexGroup = data.getStringExtra("selectIndexGroup");
			if (type != null && (type.equals("del") || type.equals("delall"))) {
				if (selectIndexGroup.length() > 0) {
					String[] array = selectIndexGroup.split(",");
					List<Map<String, Object>> temp = new ArrayList<Map<String, Object>>();
					for (int i = 0; i < array.length; i++) {
						temp.add(list.get(Integer.valueOf(array[i]).intValue()));
					}
					for (int i = 0; i < temp.size(); i++) {
						list.remove(temp.get(i));
					}
					adapter.notifyDataSetChanged();
					user_message_listview.invalidate();
				}
			}
			break;
		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);

	}

	// 点击列表中的某行
	class MyOnItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Intent intent = new Intent();
			TextView idTv = (TextView) arg1
					.findViewById(R.id.user_message_list_messageid);
			TextView idTv2 = (TextView) arg1
					.findViewById(R.id.user_message_list_userid);
			intent.putExtra("currentMessageId",
					Long.valueOf(idTv.getText() + ""));
			intent.putExtra("currentUserId", Long.valueOf(idTv2.getText() + ""));
			intent.putExtra("selectIndex", arg2);
			intent.setClass(PersonalMessageActivity.this,
					PersonalMessageMenuActivity.class);
			startActivityForResult(intent, 0);
		}

	}

	@Override
	public void setTitleViews() {
		LinearLayout titleLayout = getTITLE_LAYOUT();

		goBack = CustomTitleActivity.createTitleLeftButton(
				PersonalMessageActivity.this, R.string.reg_title_btn_goback,
				Constants.TITLE_BTN_MARGIN1);
		goBack.setOnClickListener(rbc);
		titleLayout.addView((View) goBack.getParent());

		TextView tv = CustomTitleActivity.createTitleDesc(
				PersonalMessageActivity.this, R.string.user_message_title, 0);
		titleLayout.addView((View) tv.getParent());

		reCall = CustomTitleActivity.createTitleRightButton(
				PersonalMessageActivity.this, R.string.user_message_recall,
				Constants.TITLE_BTN_MARGIN2);
		LinearLayout ll = (LinearLayout) reCall.getParent();
		ll.setGravity(Gravity.RIGHT);
		reCall.setOnClickListener(rbc);
		titleLayout.addView(ll);

	}

	@Override
	public void setLayoutId() {
		setContentView(R.layout.user_message_list);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		userId = getIntent().getLongExtra("currentUserId", 0);

		super.onCreate(savedInstanceState);
		user_message_listview = (ListView) findViewById(R.id.user_message_listview);
		// 这句代码非常重要，用来设置点击的当前项的背景
		user_message_listview.setSelector(R.drawable.list_bg);
		// 这句代码非常重要，用来设置拖动中的背景
		user_message_listview.setCacheColorHint(0);

		HttpResponseObject hro = HttpUtils.getInfoByUrlPost(
				Constants.USER_VIEWMESSAGE_LIST_URL + "userId=" + userId
						+ "&curUserId=" + Constants.USER_ID,
				PersonalMessageActivity.this);
		try {
			JSONArray array = hro.getResult().getJSONArray("list");
			list = new ArrayList<Map<String, Object>>();
			for (int i = 0; i < array.length(); i++) {
				JSONObject jsonObject = array.getJSONObject(i);
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("user_message_list_logo", jsonObject.get("userLogo"));
				map.put("user_message_list_username",
						jsonObject.get("userName"));
				map.put("user_message_list_time", jsonObject.get("publishTime"));
				map.put("user_message_list_desc", jsonObject.get("desc"));
				map.put("user_message_list_userid", jsonObject.get("userId"));
				map.put("user_message_list_messageid",
						jsonObject.get("messageId"));
				list.add(map);
			}
			adapter = new ImgSimpleAdapter(PersonalMessageActivity.this, list,
					R.layout.user_message_list_list, new String[] {
							"user_message_list_logo",
							"user_message_list_username",
							"user_message_list_time", "user_message_list_desc",
							"user_message_list_userid",
							"user_message_list_messageid" }, new int[] {
							R.id.user_message_list_logo,
							R.id.user_message_list_username,
							R.id.user_message_list_time,
							R.id.user_message_list_desc,
							R.id.user_message_list_userid,
							R.id.user_message_list_messageid });
			user_message_listview.setAdapter(adapter);
			user_message_listview
					.setOnItemClickListener(new MyOnItemClickListener());
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

}
